package com.rykj.ext

import android.content.Context
import android.content.Intent
import android.media.MediaScannerConnection
import android.util.Log
import android.util.TypedValue
import com.lcw.library.imagepicker.utils.Util
import com.rykj.util.CommonUtil
import java.io.File


/**
 * Description:
author: wangwu on 2018/10/10 10:22.
 */


fun Context.px2Dp( pxValue:Float):Int{
    val scale = resources.displayMetrics.density
    return (pxValue / scale + 0.5f).toInt()
}


fun Context.dp2Px(dpValue:Float):Int{
    val scale = resources.displayMetrics.density
    return (dpValue * scale + 0.5f).toInt()
}

fun Context.px2dp(dpValue: Float):Int{
    return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,dpValue,resources.displayMetrics).toInt()
}


fun Context.dp2px(spValue: Float):Int{
    return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,spValue,resources.displayMetrics).toInt()
}

fun Context.scanFile(file: File?) {
    if (file == null) return
    val intent = Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE)
    val uri = Util.getUriForFile(this, file)
    intent.data = uri
    sendBroadcast(intent)

    val files = arrayOf(file.absolutePath)
    MediaScannerConnection.scanFile(this, files, null) { path, uri ->
        Log.d("SCANNER", "scanFile() called with: path = [$path], uri = [$uri]")
    }
}




fun Context.showToast(msg: String) {
    CommonUtil.showToast(msg, this)
}



